import React from 'react'
import DownloadHint from "../../shared-components/DownloadHint/DownloadHint";
import {GlobalStore} from "../../context";

const withDownload = (Component) => {
    const wrapper = class extends React.Component {
        static contextType = GlobalStore

        constructor(props) {
            super(props)
            this.state = {
                downloadShow: true,
            }
        }

        componentDidMount() {
            this.setDownloadShow()
        }

        setDownloadShow = () => {
            const downloadShow = this.context.downloadApp.show
            this.setState({
                downloadShow,
            })
        }

        hidden = () => {
            this.context.downloadApp.hidden()
            this.setState({
                downloadShow: false,
            })
        }

        renderDownloadHint = (config = {}) => {
            const download = this.state.downloadShow
            return download
                ? <DownloadHint onHidden={this.hidden} level={config?.level}/>
                : null
        }

        render() {
            const self = this
            return <React.Fragment>
                <Component
                    downloadShow={self.state.downloadShow}
                    renderDownloadHint={this.renderDownloadHint}
                    {...this.props}
                />
            </React.Fragment>
        }
    }
    return wrapper
}

export default withDownload